package raceReservationServer.loginGateway;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;

import authorizationRMI.IAuthorizationRMI;
import authorizationRMI.InvalidPasswordException;
import authorizationRMI.InvalidUserException;
import authorizationRMI.Runner;


public class AuthRMIGateway implements IAuthorizeGateway {
	/**
	 * Add your code to invoke the Authorization RMI Server here Remember you
	 * must use the interface and exceptions contained in
	 * AuthorizationRMIClient.jar THIS SECTION BELONGS TO THE FIRST ASSIGNMENT
	 */
	
	private IAuthorizationRMI iauth;
	
	public AuthRMIGateway(String serviceUri) {
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		try {
			String name = serviceUri;
			iauth = (IAuthorizationRMI) Naming.lookup(name);

		} catch (Exception e) {
			System.err.println("Error in Authorization Manager.  getAuthManager(): " + e.getMessage());
		}
	}

	public RunnerDO login(String user, String pass) throws ValidationException {
		RunnerDO runner = null;
		Runner run;
		
		try
		{
			run = iauth.login(user,pass);
			System.out.println("The details of the runner are: Name:" + run.getName() + " - Age: " + run.getAge() + " - Sex: " + run.getSex());
			runner = new RunnerDO(run.getName(), run.getPassword(), run.getName(), run.getAge(), run.getSex());
		}
		catch (InvalidUserException e) {System.out.println("Invalid Username Exception");}
		catch (InvalidPasswordException e) {System.out.println("Invalid Password Exception");}
		catch (RemoteException e) {e.printStackTrace();}
				
		return runner;
	}
}
